(function () {
	var req, res, parameters, writer;
	
	api = {};
	api.hello = {
		say: function(params) {
			if (params) {
				return "Hello, " + params.name + "!";
			} else {
				return "Hello!";
			}		
		}
	};

	req = HttpServletRequest({
		getParameter: function(name) {
			return parameters[name] || null;
		}
	});

	res = HttpServletResponse({
		getWriter: function(){
			return writer;
		}
	});
	
	testAPI = {
		name: function() {
			return "API Servlet Test";
		},
		testWithRequest: function() {
			var name = "Pei Xiaoxing";
			var request = {
				action: "hello.say",
				params: { name: name }
			};

			parameters = {
				request: JSON.stringify(request)
			};
	
			var output = ByteArrayOutputStream();
			writer = PrintWriter(output);

			servlets.api.service(req, res);
			writer.flush();

			var expected = api.hello.say({ name: name });
			var actual = JSON.parse(output.toString()).data;
			Assert.assertEquals(expected, actual);
		},
		testWithParameters: function() {
			parameters = { action: "hello.say" };

			var output = ByteArrayOutputStream();
			writer = PrintWriter(output);

			servlets.api.service(req, res);
			writer.flush();

			var expected = api.hello.say();
			var actual = JSON.parse(output.toString()).data;
			Assert.assertEquals(expected, actual);
		}
	};
}());
